/**   
 * Copyright © 2017 金风科技. All rights reserved.
 * 
 * @Title: ResponseUtil.java 
 * @Prject: tgcw-c-common
 * @Package: com.goldwind.common.util.spring 
 * @Description: 返回值util
 * @author: liangwenlu   
 * @date: 2019年3月25日 
 * @version: V1.0   
 */
package com.gd.iot.energy.util;

import com.gd.iot.energy.code.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ResponseUtil
 * @Description: 返回值util
 */
@Component
@Slf4j
public class ResponseUtil {

	@Autowired
	private MessageUtil messageUtil;

	/**
	 * @Title: renderMsg
	 * @Description: 封装返回信息
	 * @param obj
	 * @return
	 */
	public Map<String, Object> renderMsg(Object obj) {
		return renderMsg(ResponseCode.SUCCESS, obj);
	}

	/**
	 * @Title: renderMsg
	 * @Description: 封装返回信息
	 * @param code
	 * @param obj
	 * @return
	 */
	public Map<String, Object> renderMsg(String code, Object obj) {
		return renderMsg(code, messageUtil.getMessage(code), obj);
	}

	/**
	 * @Title: renderMsg
	 * @Description: 封装返回信息
	 * @param code
	 * @return
	 */
	public Map<String, Object> renderMsg(String code, String msg) {
		return renderMsg(code, msg, null);
	}

	/**
	 * @Title: renderMsg
	 * @Description: 封装返回信息
	 * @param code 错误码
	 * @param msg 错误信息
	 * @param obj 对象
	 * @return
	 */
	public Map<String, Object> renderMsg(String code, String msg, Object obj) {
		Map<String, Object> map = new HashMap<>(4);
		map.put("code", code);
		map.put("msg", msg);
		map.put("data", obj);
		map.put("time", new Date());
		return map;
	}

	/**
	 * 下载文件
	 *
	 * @param filePath
	 * @param fileName
	 * @return
	 */
	public static void download(String filePath, String fileName) {
		HttpServletResponse response = null;
		try {
			ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			if(requestAttributes==null){
				return;
			}
			response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
			// path是指欲下载的文件的路径。
			File file = new File(filePath);

			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
	}
}
